#include "OneButton.h"
#include "conf.h"
#include "button.h"

bool callwebportal = false;

#ifdef TOGGLEBTN
bool find_toogle = false;
bool working = false;
int ticks = 0;
unsigned long _startTime;
bool level;
void IRAM_ATTR PAD_1_callback();

unsigned long getPressedMs()
{
  return (millis() - _startTime);
}

void IRAM_ATTR toggle_callback()
{
  find_toogle = true;
}

void btn_tick()
{
  if (find_toogle && !working)
  {
    find_toogle = false;
    working = true;
    _startTime = millis();
    level = digitalRead(PAD_1_PIN);
    ticks++;
    // Serial.println("find_toogle");
    return;
  }
  else if (find_toogle && working)
  {
    find_toogle = false;
    if (level != digitalRead(PAD_1_PIN))
    {
      level = digitalRead(PAD_1_PIN);
      ticks++;
      _startTime = millis();
      return;
    }
  }
  else
  {
    if (getPressedMs() > 400 && working)
    {
      working = false;
      Serial.printf("check out, tick is %d\r\n", ticks);

    switch (ticks)
    {
    case 1:
      PAD_1_callback();
      break;
    case 4:
      callwebportal = true;
      break;
    default:
      break;
    }
      ticks = 0;
    }
  }
}

void button_init()
{
  pinMode(PAD_1_PIN, INPUT_PULLUP);
  level = digitalRead(PAD_1_PIN);
  attachInterrupt(digitalPinToInterrupt(PAD_1_PIN), toggle_callback, CHANGE);
}
#endif

#ifdef PUSHBTN
#if (KEYS >= 1)
OneButton button1(PAD_1_PIN, true);
void IRAM_ATTR PAD_1_callback();
void pad_1_singleClick()
{
  PAD_1_callback();
}
void pad_1_doubleClick()
{
}
void pad_1_multiClick()
{
  int i = button1.getNumberClicks();
  if (i == 4)
  {
    callwebportal = true;
  }
}
void pad_1_pressStop()
{
  Serial.print("pressStop()......call for esp reboot");
  ESP.restart();
}
#endif
#if (KEYS >= 2)
OneButton button2(PAD_2_PIN, true);
void IRAM_ATTR PAD_2_callback();
void pad_2_singleClick()
{
  PAD_2_callback();
}
void pad_2_doubleClick()
{
}
void pad_2_multiClick()
{
}
void pad_2_pressStop()
{
  Serial.print("pressStop()......call for esp reboot");
  ESP.restart();
}
#endif
#if (KEYS >= 3)
OneButton button3(PAD_3_PIN, true);
void IRAM_ATTR PAD_3_callback();
void pad_3_singleClick()
{
  PAD_3_callback();
}
void pad_3_doubleClick()
{
}
void pad_3_multiClick()
{
}
void pad_3_pressStop()
{
  Serial.print("pressStop()......call for esp reboot");
  ESP.restart();
}
#endif
#if (KEYS >= 4)
OneButton button4(PAD_4_PIN, true);
void IRAM_ATTR PAD_4_callback();
void pad_4_singleClick()
{
  PAD_4_callback();
}
void pad_4_doubleClick()
{
}
void pad_4_multiClick()
{
}
void pad_4_pressStop()
{
  Serial.print("pressStop()......call for esp reboot");
  ESP.restart();
}
#endif

IRAM_ATTR void checkTicks()
{
  // include all buttons here to be checked
#if (KEYS >= 1)
  button1.tick();
#endif
#if (KEYS >= 2)
  button2.tick();
#endif
#if (KEYS >= 3)
  button3.tick();
#endif
#if (KEYS >= 4)
  button4.tick();
#endif
}

void button_init() {
#if (KEYS >= 1)
  attachInterrupt(digitalPinToInterrupt(PAD_1_PIN), checkTicks, CHANGE);
  button1.attachClick(pad_1_singleClick);
  button1.attachDoubleClick(pad_1_doubleClick);
  button1.attachMultiClick(pad_1_multiClick);
  button1.attachLongPressStop(pad_1_pressStop);
#endif
#if (KEYS >= 2)
  attachInterrupt(digitalPinToInterrupt(PAD_2_PIN), checkTicks, CHANGE);
  button2.attachClick(pad_2_singleClick);
  button2.attachDoubleClick(pad_2_doubleClick);
  button2.attachMultiClick(pad_2_multiClick);
  button2.attachLongPressStop(pad_2_pressStop);
#endif
#if (KEYS >= 3)
  attachInterrupt(digitalPinToInterrupt(PAD_3_PIN), checkTicks, CHANGE);
  button3.attachClick(pad_3_singleClick);
  button3.attachDoubleClick(pad_3_doubleClick);
  button3.attachMultiClick(pad_3_multiClick);
  button3.attachLongPressStop(pad_3_pressStop);
#endif
#if (KEYS >= 4)
  attachInterrupt(digitalPinToInterrupt(PAD_4_PIN), checkTicks, CHANGE);
  button4.attachClick(pad_4_singleClick);
  button4.attachDoubleClick(pad_4_doubleClick);
  button4.attachMultiClick(pad_4_multiClick);
  button4.attachLongPressStop(pad_4_pressStop);
#endif
  button1.setPressMs(3000); // that is the time when LongPressStart is called
} 
void btn_tick(){
  checkTicks();
}
#endif
